package server;

import channel.HttpChannelInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author yuzk
 * @date 2018/1/15
 */
public class HttpServer {

    public static final String IP = "localhost";
    public static final int PORT = 8666;

    /**用于分配处理业务线程的线程组个数 */
    protected static final int BUSY_GROUP_SIZE = Runtime.getRuntime().availableProcessors()*2; //默认

    /** 业务出现线程大小*/
    protected static final int BUSY_THREAD_SIZE = 4;

    private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BUSY_GROUP_SIZE);

    private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BUSY_THREAD_SIZE);

    protected static void run() throws Exception{
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new HttpChannelInitializer())
                .bind(IP, PORT)
                .sync()
                .channel()
                .closeFuture()
                .sync();


    }

    protected static void shutdown() {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

    public static void main(String[] args) throws Exception{
        System.out.println("开始启动http服务器...");
        HttpServer.run();
    }
}
